import 'package:get/get.dart';
import 'package:signage/l10n/l10n.dart';

class DeviceCachedInfo {
  final String id;
  final String name;
  DateTime lastConnected;
  final String group;
  final String? groupId;
  bool canCast;
  final List<EslDisplayInfo> displayInfo;
  final int? batteryLevel;
  String firmwareVersion;
  final String? board;
  final DateTime connectedTime;
  int? rssi;
  String? filePath;
  String? updateVersion;

  DeviceCachedInfo({
    required this.id,
    required this.name,
    required this.lastConnected,
    required this.group,
    this.groupId,
    this.canCast = false,
    required this.displayInfo,  
    this.board,
    this.batteryLevel,
    this.firmwareVersion = '',
    required this.connectedTime,
    this.rssi,
    this.filePath = '',
    this.updateVersion
  });

  factory DeviceCachedInfo.fromJson(Map<String, dynamic> json) {
    return DeviceCachedInfo(
      id: json['id'],
      board: json['board'],
      name: json['name'],
      lastConnected: DateTime.parse(json['lastConnected']),
      group: json['group'] ?? AppLocalizations.of(Get.context!)!.translate('device_default_group'),
      displayInfo: (json['displayInfo'] as List).map((e) => EslDisplayInfo.fromJson(e)).toList(),
      batteryLevel: json['batteryLevel'],
      firmwareVersion: json['firmwareVersion'],
      connectedTime: DateTime.parse(json['connectedTime']),
      rssi: json['rssi'],
      filePath: json['filePath'],
      updateVersion: json['updateVersion'],
      groupId: json['groupId'] ?? '0',
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'board': board,
      'lastConnected': lastConnected.toIso8601String(),
      'group': group,
      'displayInfo': displayInfo.map((e) => e.toJson()).toList(),
      'batteryLevel': batteryLevel,
      'firmwareVersion': firmwareVersion,
      'connectedTime': connectedTime.toIso8601String(),
      'rssi': rssi,
      'filePath': filePath,
      'updateVersion': updateVersion,
      'groupId': groupId,
    };
  }

  DeviceCachedInfo copyWith({
    String? id,
    String? name,
    String? board,
    DateTime? lastConnected,
    String? group,
    List<EslDisplayInfo>? displayInfo,
    int? batteryLevel,
    String? firmwareVersion,
    DateTime? connectedTime,
    int? rssi,
    bool? canCast,
    String? filePath,
    String? groupId,
  }) {
    return DeviceCachedInfo(
      id: id ?? this.id,
      name: name ?? this.name,
      board: board ?? this.board,
      lastConnected: lastConnected ?? this.lastConnected,
      group: group ?? this.group,
      displayInfo: displayInfo ?? this.displayInfo,
      batteryLevel: batteryLevel ?? this.batteryLevel,
      firmwareVersion: firmwareVersion ?? this.firmwareVersion,
      connectedTime: connectedTime ?? this.connectedTime,
      rssi: rssi ?? this.rssi,
      canCast: canCast ?? this.canCast,
      filePath: filePath ?? this.filePath,
      groupId: groupId ?? this.groupId,
    );
  }
} 


class EslDisplayInfo {
  final int width;
  final int height;
  final int displayType;

  EslDisplayInfo({
    required this.width,
    required this.height,
    required this.displayType,
  });

  // 从字节数组解析
  factory EslDisplayInfo.fromBytes(List<int> bytes) {
    if (bytes.length < 5) {
      throw Exception('Invalid data length for ESL Display Info');
    }

    // 解析宽度 (2 bytes, little endian)
    final width = bytes[0] | (bytes[1] << 8);
    
    // 解析高度 (2 bytes, little endian)
    final height = bytes[2] | (bytes[3] << 8);
    
    // 解析显示类型 (1 byte)
    final displayType = bytes[4];

    return EslDisplayInfo(
      width: width,
      height: height,
      displayType: displayType,
    );
  }

  // 获取显示类型描述
  String get displayTypeDescription {
    switch (displayType) {
      case 0x01:
        return 'Black & White';
      case 0x02:
        return '3 Gray Scale';
      case 0x03:
        return '4 Gray Scale';
      case 0x04:
        return '8 Gray Scale';
      case 0x05:
        return '16 Gray Scale';
      case 0x06:
        return 'Red/Black/White';
      case 0x07:
        return 'Yellow/Black/White';
      case 0x08:
        return 'Red/Yellow/Black/White';
      case 0x09:
        return '7 Color';
      case 0x0A:
        return '16 Color';
      case 0x0B:
        return 'Full RGB';
      default:
        return 'Unknown Type ($displayType)';
    }
  }

  @override
  String toString() {
    return '分辨率: ${width}x${height}, 类型: $displayTypeDescription';
  }

  Map<String, dynamic> toJson() => {
    'width': width,
    'height': height,
    'colorDepth': displayType,
    'refreshRate': 60, // 假设刷新率为60Hz
    'displayType': displayType,
  };

  factory EslDisplayInfo.fromJson(Map<String, dynamic> json) {
    return EslDisplayInfo(
      width: json['width'] as int,
      height: json['height'] as int,
      displayType: json['displayType'] as int,
    );
  }
} 